Ext.ns('SM.Explanation')


SM.Explanation.buildShowButton=function(){
	 var canlce ={text:'关闭',handler:function(){
	         this.ownerCt.ownerCt.close();
	     }};
	 if (loginUser.userId =="luoxw") {
	 	return [
          {text:'编辑',handler:function(n){
              var explanationId =this.ownerCt.ownerCt.explanationId;
              SM.Explanation.update(explanationId);
          }},canlce
      ]
	 } else {
	 	return [canlce]
	 }
     
};
/**
 * 说明展示页面
 * @class SM.Explanation.Show
 * @extends Ext.Window
 */
SM.Explanation.Show =Ext.extend(Ext.Window,{
	  width:'700'
	  ,height:'500'
	  ,labelAlign:'top'
	  ,title:'表说明'
	  ,maximizable:true
	  ,resizable:false
	  ,modal:false
	  ,content:''
	  ,buttons:SM.Explanation.buildShowButton()
	  ,listeners:{
	  	resize:function(t){
	  		if (this.editorEL){
	  			var heigth= this.getInnerHeight();
		     	this.editorEL.style.height=heigth+'px';
		     	this.editorEL.children[0].style.height=heigth+'px';
	  		}
	     }
	    ,show:function(t){
	  		if (this.editorEL){
		     	this.editorEL.style.height=this.getInnerHeight()+'px';
		     	this.editorEL.children[0].style.height=this.getInnerHeight()+'px';
	  		}
	     }
	     ,destory:function(t){
	     	if (this.editor) {
	     		this.editor.remove();
	     	}
	     }
	  
	  }
	  ,buttonAlign:'center'
	  
      ,initComponent:function(){
      	this.items =[{
      	              xtype:'textarea'
      	              ,width:'100%'
      	              ,height:'435'
      	              ,listeners : {
							afterrender : function(t) {
								var me =this;
								var editor;
								var id = t.id;
								editor = KindEditor.create('#' + t.id, {
									resizeType : 0,
									allowPreviewEmoticons : false,
									allowImageUpload : false,
									items:[]
								});
								editor.html(me.ownerCt.content);
								var medom =me.getEl().dom.previousSibling;
								medom.removeChild(medom.children[0]);
								medom.removeChild(medom.children[1]);
								editor.readonly();
								me.ownerCt.editor =editor;
								me.ownerCt.editorEL=medom.children[0];
								}
						}
      	              }
      	              ]
      	SM.Explanation.Show.superclass.initComponent.call(this);
      }
});
/**
 * 说明新增和更新页面
 * @class SM.Explanation.Edit
 * @extends Ext.Window
 */
SM.Explanation.Edit =Ext.extend(Ext.Window,{
	  width:'760'
	  ,height:'500'
	  ,layout:'form'
	  ,title:'说明编辑'
	  ,labelAlign:'top'
	  ,resizable:false
	  ,maximizable:true
	  ,modal:true
	  ,padding:10
	  ,listeners:{
		  destory:function(t){
		     	if (this.editor) {
		     		this.editor.remove();
		     	}
		     }
		 ,resize:function(t){
	  		if (this.editor){
	  			var padding =this.padding||0;
	  			var innerwidth =this.getInnerWidth();
	  			var innerheight =this.getInnerHeight();
	  			var items =Ext.query('div.x-form-item',this.id);
	  			var items0H =items[0].offsetHeight ;
	  			var items1H =items[1].childNodes[0].offsetHeight ;
	  			this.editor.resize(innerwidth-padding*2,innerheight-items0H-items1H-padding*3);
	  		}
	     }
	    ,show:function(t){
	  		if (this.editor){
	  			var padding =this.padding||0;
	  			var innerwidth =this.getInnerWidth();
	  			var innerheight =this.getInnerHeight();
	  			var items =Ext.query('div.x-form-item',this.id);
	  			var items0H =items[0].offsetHeight ;
	  			var items1H =items[1].childNodes[0].offsetHeight ;
	  			this.editor.resize(innerwidth-padding*2,innerheight-items0H-items1H-padding*3);
	  		}
	  		this.getComponent(0).clearInvalid();	
	     }  
	  }
	  ,buttons:[{text:'保存',handler:function(){
	  	        var win =this.ownerCt.ownerCt;
	  	        win.editor.sync();
	  	        if (!win.getComponent(0).isValid()){
	  	        	Ext.Msg.alert('提示','说明名称'+win.getComponent(0).getActiveError());
	  	        	return
	  	        }
	  	        var explanationName =win.getComponent(0).getValue();
	  	        var explanationContent =win.getComponent(1).getValue();
	  	        var explanationRemark =win.editor.text().substring(0,150);
	  	        //去掉img标识
	  	        explanationRemark=explanationRemark.replace(/<.*\/>/g,"");
	  	        var flag =win.flag||"1";
	  	        var url ='explanation_save.action'
	  	        var params ={'tpSmExplanation.explanationName':explanationName
	                        ,'tpSmExplanation.explanationContent':explanationContent
	                        ,'tpSmExplanation.flag':flag
	                        ,'tpSmExplanation.explanationRemark':explanationRemark}          
	  	        if (win.explanationId){
	  	        	url ='explanation_update.action'
	  	        	params['tpSmExplanation.explanationId'] =win.explanationId;
	  	        }
	            Ext.Ajax.request({
	                params:params
	                ,method:'post'
	                ,url:url
	                ,success:function(result,request){
	                	var page =Ext.decode(result.responseText);
	                	if (page.status ==200){
		                	if(!win.explanationId) {
		                		win.explanationId =page.totalProperty;
		                		MsgTip.msg({
									 	msg:"新增成功",
									 	cmp:win,
									 	pos:"center"
							  	});
		                	} else {
		                		MsgTip.msg({
									 	msg:"保存成功",
									 	cmp:win,
									 	pos:"center"
							  	});
		                	}
		                	win.reflash =true;
	                	}
	                }
	            });   
	  }},{text:'关闭',handler:function(){
	  	     var win =this.ownerCt.ownerCt;
	  		 if (win.reflash) {
	  		 	if (win.store!=null){
	  		 		win.store.load({params:{start:0,limit:win.store.limit}});
	  		 	}
	  		 }
	         win.close();
	  }}]
	  ,buttonAlign:'center'
      ,initComponent:function(){
      	var explanationName =this.explanationName||"";
      	var explanationContent = this.explanationContent||"";
      	this.items =[{xtype:'textfield'
      	              ,fieldLabel:'说明名称'
      	              ,width:'300'
      	              ,allowBlank:false
      	              ,maxLength:100
      	              ,value:explanationName
      	              }
      	             ,{
      	              xtype:'textarea'
      	              ,width:'100%'
      	              ,height:'330'
      	              ,fieldLabel:'说明内容'
      	              ,listeners : {
							afterrender : function(t) {
								var me =this;
								var editor;
								var id = t.id;
									editor = KindEditor.create('#' + t.id, {
										resizeType : 0,
										allowPreviewEmoticons : false,
										allowImageUpload : true,
										filePostName:'file',
										formatUploadUrl:false,
										basePath:'js/kindeditor/',
										uploadJson:'upload_keUpload.action',
										items:['source','|','undo','redo','|','fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline','strikethrough',
											   '|','wordpaste','quickformat','removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
											   'insertunorderedlist', '|','table','hr','emoticons', 'image', 'link','|','about']
									});
									editor.html(explanationContent);
									t.ownerCt.editor =editor;
								}
						}
      	              }
      	              ]
      	SM.Explanation.Edit.superclass.initComponent.call(this);
      }
});

/**
 * 展示说明的接口函数
 * @param {} explanationId 要展示的Id
 */
SM.Explanation.showExplanation =function(explanationId) { 
		Ext.Ajax.request({
                params:{'tpSmExplanation.explanationId':explanationId}
                ,method:'post'
                ,url:'explanation_get.action'
                ,success:function(result,request){
	                	var page =Ext.decode(result.responseText);
	                	var tpSmExplanation =page.data;
	                	new SM.Explanation.Show({
	                		   explanationId:explanationId
	                	       ,title:tpSmExplanation.explanationName
	                	       ,content:tpSmExplanation.explanationContent
	                	}).show();
	                }
           });
}

/**
 * 更新
 * @param {} t
 */
SM.Explanation.update =function(t){
	var explanationId ="";
	var store=null;
	if (Ext.isString(t)||Ext.isNumber(t)||Ext.isBoolean(t)){
	      explanationId=t;
	} else {
		var grid =t.ownerCt.ownerCt;       
		var recs =grid.getSelectionModel().getSelections()
		if (recs.length==0) {
			return;
		}
		explanationId=recs[0].get('explanationId');
		store =grid.store;
	}
	Ext.Ajax.request({
                params:{'tpSmExplanation.explanationId':explanationId}
                ,method:'post'
                ,url:'explanation_get.action'
                ,success:function(result,request){
	                	var page =Ext.decode(result.responseText);
	                	if (page.status ==200){
	                	var tpSmExplanation =page.data;
	                	 new SM.Explanation.Edit({
	                	     explanationId:tpSmExplanation.explanationId
	                	     ,explanationName:tpSmExplanation.explanationName
	                	     ,explanationContent:tpSmExplanation.explanationContent
	                	     ,flag:tpSmExplanation.flag
	                	     ,title:'说明编辑'
	                	     ,store:store
	                	}).show()
	                	}
	                }
           });                      
}